package simple_bbs.model;

import act.app.conf.AutoConfig;
import act.db.morphia.MorphiaAdaptiveRecord;
import act.db.morphia.MorphiaDao;
import org.apache.bval.constraints.NotEmpty;
import org.joda.time.DateTime;
import org.mongodb.morphia.annotations.Entity;
import org.osgl.$;
import org.osgl.util.Const;

@Entity("msg")
public class Message extends MorphiaAdaptiveRecord<Message> implements UserLinked {

    // Store the author's email
    public String author;

    @NotEmpty
    public String subject;

    @NotEmpty
    public String body;

    public DateTime getTimestamp() {
        return _lastModified();
    }

    @Override
    public String userId() {
        return author;
    }

    @AutoConfig
    public static class Dao extends MorphiaDao<Message> {

        public static final Const<Integer> PAGE_SIZE = $.constant();

        public Iterable<Message> fetch(int page) {
            int pageSize = PAGE_SIZE.get();
            return q().limit(pageSize).offset(pageSize * page).order("-_modified").fetch();
        }
    }
}
